/**
 * 
 */
package uniandes.dermatologia.training.activities;

import rajawali.RajawaliActivity;
import uniandes.dermatologia.R;
import uniandes.dermatologia.models.PhysicalExamination;
import uniandes.dermatologia.resources.Render;
import uniandes.dermatologia.singleton.DermatologiaUniandesApp;
import uniandes.dermatologia.training.CaseLogic;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.PointF;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;

/**
 * @author Julio
 *
 */
@SuppressWarnings("deprecation")
public class BodyCaseActivity extends RajawaliActivity {

	private ProgressBar loaderGraphic;
	private Render renderer;
	private View.OnTouchListener buttonListener;

	PointF start = new PointF();
	PointF mid = new PointF();
	float oldDist = 1f;
	boolean rotando = false;

	private RelativeLayout relativeLayout;
	private Button btnRight;
	private Button btnLeft;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		getActionBar().setDisplayHomeAsUpEnabled(false);

		setGLBackgroundTransparent(true);
		renderer = new Render(this);
		mLayout.setBackgroundColor(Color.parseColor("#E6E6E6"));
		
		DermatologiaUniandesApp app = (DermatologiaUniandesApp)getApplication();
		
		CaseLogic caseLogic = app.getCurrentCaseLogic();
		
		PhysicalExamination examination = caseLogic.getExamination();
		
		boolean[] affectedAreas = examination.getAffectedAreas();
		
		
		
		renderer.setSurfaceView(mSurfaceView);
		super.setRenderer(renderer);

		LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
		relativeLayout = (RelativeLayout) inflater.inflate(
				R.layout.activity_body, null);

		mLayout.addView(relativeLayout);

		renderer.setEstaSeleccionadoModelo(affectedAreas);
		loaderGraphic = (ProgressBar) findViewById(R.id.loaderBody);
		btnRight = (Button) findViewById(R.id.btnRight);
		btnLeft = (Button) findViewById(R.id.btnLeft);
		buttonListener = new OnTouchListener() {

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				switch (event.getAction() & MotionEvent.ACTION_MASK) {
				case MotionEvent.ACTION_DOWN:
					if (v.getId() == R.id.btnRight) {
						renderer.iniciarAnimacionDerecha();
					} else if (v.getId() == R.id.btnLeft) {
						renderer.iniciarAnimacionIzquierda();
					}
					break;
				case MotionEvent.ACTION_UP:
					if (v.getId() == R.id.btnRight) {
						renderer.detenerAnimacionDerecha();
					} else if (v.getId() == R.id.btnLeft) {
						renderer.detenerAnimacionIzquierda();
					}
					break;
				}
				return false;
			}

		};

		btnRight.setOnTouchListener(buttonListener);
		btnLeft.setOnTouchListener(buttonListener);

	}
	
	public void hideLoader() {

		relativeLayout.post(new Runnable() {
			@Override
			public void run() {
				loaderGraphic.setVisibility(View.INVISIBLE);

				btnRight.setVisibility(View.VISIBLE);
				btnLeft.setVisibility(View.VISIBLE);
				mLayout.removeView(loaderGraphic);
				renderer.pickObjects();
			}
		});
	}

	public void onClickBack(View v) {

		finish();

	}

	public void onClickNext(View v) {
		Intent intent = new Intent(this, BorderCaseActivity.class);
		startActivity(intent);
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		super.onPause();
		DermatologiaUniandesApp app = (DermatologiaUniandesApp)getApplication();
		app.stopTimer();
	}
	
	/*
	 * (non-Javadoc)
	 * @see android.app.Activity#onResume()
	 */
	@Override
	protected void onResume() {
		super.onResume();
		DermatologiaUniandesApp app = (DermatologiaUniandesApp)getApplication();
		app.startTimer();
	}

}
